/*
 *     This file is part of Mobile GPS Logger.
 * 
 *     Mobile GPS Logger is free software: you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 * 
 *     Mobile GPS Logger is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License v3 for more details.
 * 
 *     You should have received a copy of the GNU General Public License v3
 *     along with Mobile GPS Logger. If not, see <http://www.gnu.org/licenses/>.
 */


package cx.ath.skyflyer.gpslog;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;

/**
 *
 * @author Ahti Legonkov
 */
public class SettingsForm extends Form {

    public SettingsForm(MIDlet midlet, Displayable prev) {
        super("Settings");
        this.midlet = midlet;
        this.prev = prev;

        // start logging [immediately, on user action]
        loggingStartType = new ChoiceGroup("Start logging", ChoiceGroup.EXCLUSIVE, new String[] { "immediately", "on user action" }, null);
        loggingStartType.setSelectedIndex(0, true);
        try {
            if (!Configuration.getLoggingstartType())
                loggingStartType.setSelectedIndex(1, true);
        }
        catch (Exception ex) {
        }

        this.append(loggingStartType);

        StringItem si = new StringItem("Save", "Save", StringItem.BUTTON);
        si.addCommand(CMD_SAVE);
        si.setItemCommandListener(new ItemCommandListener() {

            public void commandAction(Command c, Item i) {
                if (c == CMD_SAVE)
                    onSave();
            }
        });
        this.append(si);

        // lcoation of log file
        //final String dir = 
        //this.append(new StringItem("Log file location", dir, StringItem.));

        // log file name format
        /*final String[] formats = {
            "YYYY-MM-DD-hh-mm-ss.txt",
            "YYYY-MM-DD/hh-mm-ss.txt",
        };
        cg = new ChoiceGroup("Log file name format", ChoiceGroup.EXCLUSIVE, formats, null);
        cg.setSelectedIndex(0, true);
        this.append(cg);*/

        // filter gps sentences

        this.addCommand(CMD_BACK);
        this.setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d) {
                if (c == CMD_BACK)
                    onBack();
            }
        });
    }

    /** Command handler for CMD_BACK. */
    private void onBack() {
        goBack();
    }

    /** Command handler for CMD_SAVE. */
    private void onSave() {
        save();
        goBack();
    }

    private void save() {
        try {
            int idx = loggingStartType.getSelectedIndex();
            if (idx == 0)
                Configuration.setLoggingStartType(true);
            else
                Configuration.setLoggingStartType(false);
        }
        catch (ConfigurationException ex) {
            Alert a = new Alert("Error");
            a.setTimeout(5000);
            a.setString(ex.getMessage());
            a.setType(AlertType.ERROR);
            Display.getDisplay(midlet).setCurrent(a);
        }
    }

    /** Go back to previous Displayable. */
    private void goBack() {
        Display.getDisplay(midlet).setCurrent(prev);
    }

    private final MIDlet midlet;
    private final Displayable prev;

    private final ChoiceGroup loggingStartType;
    private static final Command CMD_BACK = new Command("Back", "Back", Command.BACK, 0);
    private static final Command CMD_SAVE = new Command("Save", "Save", Command.SCREEN, 1);
}
